﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModEmployee
{
    public partial class EmpContractAdd : System.Web.UI.Page
    {
        EmpContractBiz empContractBiz = new EmpContractBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();
        string contractID = "0";//By Update
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["ContractID"] != null)
            {
                contractID = Request.Params["ContractID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnEnd.Enabled = false;              
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                DateTime dt = System.DateTime.Now;
                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的合同信息吗？");
                new DefDicBiz().InitDropDownList("Admin_Contract_Type", ddlTypeID, "");
                txtMakeTime.Text = dt.ToString("yyyy-MM-dd");
                txtBeginTime.Text = dt.ToString("yyyy-MM-dd");
               // txtEndTime.Text = dt.AddYears(1).ToString("yyyy-MM-dd"); 
                if (contractID != "0") BindData(contractID);

            }
        }
        private void BindData(string contractID)
        {
            DataTable dt = empContractBiz.GetOneInfo(contractID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();              
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();


                string MakeTime = dt.Rows[0]["MakeTime"].ToString();
                if (Utils.RegexRule.IsDate(MakeTime))
                    txtMakeTime.Text = Convert.ToDateTime(MakeTime).ToString("yyyy-MM-dd");
                else txtMakeTime.Text = MakeTime;

                string BeginTime = dt.Rows[0]["BeginTime"].ToString();
                if (Utils.RegexRule.IsDate(BeginTime))
                    txtBeginTime.Text = Convert.ToDateTime(BeginTime).ToString("yyyy-MM-dd");
                else txtBeginTime.Text = BeginTime;

                string EndTime = dt.Rows[0]["EndTime"].ToString();
                if (Utils.RegexRule.IsDate(EndTime))
                    txtEndTime.Text = Convert.ToDateTime(EndTime).ToString("yyyy-MM-dd");
                else txtEndTime.Text = EndTime;


                new DefDicBiz().InitDropDownList(ddlTypeID, dt.Rows[0]["TypeID"].ToString());
                txtContractNo.Text = dt.Rows[0]["ContractNo"].ToString();
                txtContractName.Text = dt.Rows[0]["ContractName"].ToString();

                txtContractContent.Text = dt.Rows[0]["ContractContent"].ToString();              
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();


                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string bTime = txtBeginTime.Text.Trim();
            if (!RegexRule.IsDate(bTime))
            {
                JSUtility.Alert(this, "开始时间无效");
                return;
            }
            string eTime = txtEndTime.Text.Trim();
            if (!RegexRule.IsDate(eTime))
            {
                JSUtility.Alert(this, "结束时间无效");
                return;
            }

           string contractNo= txtContractNo.Text.Trim();
           if (empContractBiz.IsExistContractNo(contractNo))
           {
               JSUtility.Alert(this, "合同编号已经存在，请输入另外的编号");
               return;
           }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text);
            sdList.Add("DeptName", txtDeptName.Text);
            sdList.Add("TypeID", ddlTypeID.SelectedValue);           
            sdList.Add("MakeTime", txtMakeTime.Text.Trim());
            sdList.Add("BeginTime", bTime);
            sdList.Add("EndTime", eTime);
            sdList.Add("ContractNo", contractNo);
            sdList.Add("ContractName", txtContractName.Text);
            sdList.Add("ContractContent", txtContractContent.Text);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);

            if (empContractBiz.Insert("Admin_Contract", sdList))
                JSUtility.AlertAndRedirect("增加合同信息成功", "EmpContractAdd.aspx");
            else
                JSUtility.Alert(this, "增加合同信息失败");

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (contractID == "0")
            {
                JSUtility.Alert(this, "无法操作，缺少数据");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string bTime = txtBeginTime.Text.Trim();
            if (!RegexRule.IsDate(bTime))
            {
                JSUtility.Alert(this, "开始时间无效");
                return;
            }
            string eTime = txtEndTime.Text.Trim();
            if (!RegexRule.IsDate(eTime))
            {
                JSUtility.Alert(this, "结束时间无效");
                return;
            }

            string contractNo = txtContractNo.Text.Trim();
            if (empContractBiz.IsExistContractNo(contractNo))
            {
                JSUtility.Alert(this, "合同编号已经存在，请输入另外的编号");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ContractID", contractID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("TypeID", ddlTypeID.SelectedValue);
            sdList.Add("ContractNo", contractNo);
            sdList.Add("DeptName", txtDeptName.Text);
            sdList.Add("EmpName", txtEmpName.Text);
            sdList.Add("ContractName", txtContractName.Text);
            sdList.Add("MakeTime", txtMakeTime.Text.Trim());
            sdList.Add("BeginTime", bTime);
            sdList.Add("EndTime", eTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("ContractContent", txtContractContent.Text);
            sdList.Add("Account", account);

            if (empContractBiz.Update("Admin_Contract", sdList))
                JSUtility.Alert(this, "更新合同信息成功");
            else
                JSUtility.Alert(this, "更新合同信息失败");

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (contractID != "0")
            {
                if (empContractBiz.DeleteOneInfo(contractID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnEnd_Click(object sender, EventArgs e)
        {
            if (contractID == "0")
            {
                JSUtility.Alert(this, "无法操作，缺少数据");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ContractID", contractID);
            sdList.Add("Statu", "N");
            sdList.Add("Statu", System.DateTime.Now.ToString("yyyy-MM-dd")); 
            sdList.Add("Memo", txtMemo.Text.Trim());
            if (empContractBiz.Update("Admin_Contract", sdList))
                JSUtility.Alert(this, "该合同信息已经设置为失效");
            else
                JSUtility.Alert(this, "设置合同信息失效失败");
        }

       
    }
}